package com.zhongkun.datahome.web.rest;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.zhongkun.datahome.common.core.domain.R;
import com.zhongkun.datahome.service.req.IdsBody;
import com.zhongkun.datahome.service.req.cmd.sku.SkuCmd;
import com.zhongkun.datahome.service.req.cmd.sku.SwitchSkuCmd;
import com.zhongkun.datahome.service.req.qry.sku.SkuQry;
import com.zhongkun.datahome.service.res.PageVo;
import com.zhongkun.datahome.service.res.sku.SkuVo;
import com.zhongkun.datahome.service.service.ISkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhongkun.datahome.common.annotation.Log;
import com.zhongkun.datahome.common.core.controller.BaseController;
import com.zhongkun.datahome.common.enums.BusinessType;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品信息Controller
 * 
 * @author ruoyi
 * @date 2025-04-14
 */
@Api(value = "商品信息管理")
@RestController
@RequestMapping("/core/sku")
public class SkuController extends BaseController {
    @Resource
    private ISkuService skuService;

    @ApiOperation("分页查询商品信息")
    @PreAuthorize("@ss.hasPermi('core:sku:page')")
    @GetMapping("/page")
    public R<PageVo<SkuVo>> page(SkuQry qry) {
        return R.ok(skuService.pageSku(qry));
    }

    @ApiOperation("获取商品信息详细信息")
    @PreAuthorize("@ss.hasPermi('core:sku:query')")
    @GetMapping(value = "/{id}")
    public R<SkuVo> getInfo(@PathVariable("id") Long id) {
        return R.ok(skuService.selectSkuById(id));
    }

    @ApiOperation("新增商品信息")
    @PreAuthorize("@ss.hasPermi('core:sku:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> add(@Valid @RequestBody SkuCmd cmd) {
        return R.ok(skuService.addSku(cmd));
    }

    @ApiOperation("修改商品信息")
    @PreAuthorize("@ss.hasPermi('core:sku:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Valid @RequestBody SkuCmd cmd) {
        skuService.updateSku(cmd);
        return R.ok();
    }

    @ApiOperation("上/下架商品")
    @PreAuthorize("@ss.hasPermi('core:sku:switch')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping("/switch")
    public R<Void> switchByIds(@RequestBody SwitchSkuCmd cmd) {
        skuService.switchByIds(cmd);
        return R.ok();
    }

    @ApiOperation("删除商品信息")
    @PreAuthorize("@ss.hasPermi('core:sku:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping
    public R<Void> remove(@RequestBody IdsBody idsBody) {
        skuService.deleteSkuByIds(idsBody.getIds());
        return R.ok();
    }

    @ApiOperation("导入商品信息")
    @PreAuthorize("@ss.hasPermi('core:sku:import')")
    @Log(title = "商品信息", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public R<Void> upload(MultipartFile file) {
        skuService.upload(file);
        return R.ok();
    }
}
